package com.azunitech.jms.repos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.ejb.Ejb3Configuration;

import com.azunitech.jms.common.IPartRepository;
import com.azunitech.jms.common.IRepository;
import com.azunitech.jms.common.domains.LocationVo;
import com.azunitech.jms.common.domains.PartsEntity;

public class PartsRepository extends DefaultRepository implements IPartRepository{
	private PartsService m_PartsService;

	@Override
	protected ServiceBase getService( EntityManager em) {
		m_PartsService = new PartsService(em);;
		return m_PartsService;
	}

	public PartsEntity getParts(String partNumber) {
		m_EntityManager.getTransaction().begin();
        Parts parts = this.m_PartsService.createParts(partNumber);
        m_EntityManager.getTransaction().commit();
        
        PartsEntity entity = new PartsEntity(parts.getId());
        for( Location location : parts.getLocations()){
        	LocationVo vo = new LocationVo(location.getId());
        	vo.increaseQuantity(location.getQuantity());
        	entity.addLocation( vo);
        }
		return entity;
	}


	public PartsEntity getParts(String partNumber, Collection<LocationVo> locations) {
        m_EntityManager.getTransaction().begin();
        Parts parts = this.m_PartsService.createParts(partNumber, locations);
        
        m_EntityManager.getTransaction().commit();
        
        PartsEntity entity = new PartsEntity(parts.getId());
        for( Location location : parts.getLocations()){
        	LocationVo vo = new LocationVo(location.getId());
        	vo.increaseQuantity(location.getQuantity());
        	entity.addLocation(vo);
        }
        return entity;
	}

	@Override
	protected List<Class<?>> AnnotatedClasses() {
		List<Class<?>> list = new ArrayList<Class<?>>();
		list.add(Parts.class);
		list.add(Location.class);
		return list;
	}

}
